package jeecg.business.thread.task.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.thread.task.ExecTask;

import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.CommonHtmlCleanUtil;
import org.jeecgframework.core.util.clean.DongfangCaiwuHtmlCleanUtil;
import org.jeecgframework.core.util.clean.PeopleHtmlCleanUtil;
import org.jeecgframework.core.util.clean.QQVideoHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
/**
 * 
 * <p>
 * 标题: 公用抓取任务
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-6 下午2:56:28
 */
public class QqVideoExecTask extends ExecTask{
	
	public static String QQ_VIDEO_URL = "http://s.video.qq.com/search?comment=0&stype=0&plat=2&otype=json&query={keyword}&cur={page}&page=20";
	
	protected int current = 0;
	
	protected String cookies;

	public QqVideoExecTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site) {
		super(enter , keyword, site);
	}
	
	public void setCookies(){}
	
	public String getFetchHtml() {
		String basehref = QQ_VIDEO_URL;
		String word;
		try {
			word = URLEncoder.encode(keyword.getKeyword(),site.getWyzfj());
			basehref = basehref.replace(KEYWORD_FLAG, word);
			return basehref;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			return null;
		}
	}
	
	public void run(){
		CollLogEntity log = null;
		log = saveLog();
		try{
			System.out.println("exec qq video task......");
			setCookies();
			do{
				String basehref = getFetchHtml();
				basehref = basehref.replace(PAGE_FLAG , current+"");
				current++;
				if(StringUtil.isNotEmpty(basehref)){
					HtmlParseResult result = QQVideoHtmlCleanUtil.fetchQQVideo4Html(basehref, "utf-8", site, null);
					
					if(result == null || result.getChildResultList() == null) return;
					
					HtmlParseResult childResult = null;
					KeyinfoEntity keyinfo = null;
					for(HtmlParseResult child : result.getChildResultList()){
						if(keyinfoService.hasHref(child.getUrl() , enter.getId())) continue;
						try{
							childResult = child;
//							System.out.println(childResult.getTitle());
							childResult.setCharset(site.getWyzfj());
							childResult.setType(site.getJclx());
							childResult.setLy(site.getZddz());
							childResult.setCjzd(site.getZddz());
							childResult.setSite(site.getMc());
							childResult.setKeyword(keyword.getKeyword());
							childResult.setPcfs(site.getPcfs());
							childResult.setBodycontent(childResult.getTitle());
							keyinfo = new KeyinfoEntity(childResult);
							keyinfo.setQyid(enter.getId());
							if(filter(enter, keyinfo , keyword))		//过滤
								keyinfoService.saveKeyinfo(keyinfo);
						}catch(Exception e){
//							e.printStackTrace();
						}
					}
				}
			}while(current<maxpage);
		}catch(Exception e){
			e.printStackTrace();
//				log.setSfcg("2");
//				log.setCwxx(e.getMessage());
		}finally{
			log = collLogService.get(CollLogEntity.class, log.getId());
			log.setJssj(new Date());
			collLogService.updateEntitie(log);
			System.out.println("end....");
		}			
	}

}
